/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved. 
 */
package com.icinfo.cloud.provider.punish.csource.manage.service.impl;

import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.common.enums.FlowStageTypeEnum;
import com.icinfo.cloud.provider.punish.csource.manage.dto.FlowCommonDto;
import com.icinfo.cloud.provider.punish.csource.manage.dto.FlowRecordDto;
import com.icinfo.cloud.provider.punish.csource.manage.dto.FlowRecordVo;
import com.icinfo.cloud.provider.punish.csource.manage.mapper.CaseSourceFlowRecordMapper;
import com.icinfo.cloud.provider.punish.csource.manage.model.AppointedPersonInfo;
import com.icinfo.cloud.provider.punish.csource.manage.model.CaseSourceFlowRecord;
import com.icinfo.cloud.provider.punish.csource.manage.service.ICaseSourceFlowRecordService;
import com.icinfo.framework.core.service.support.MyBatisServiceSupport;
import com.icinfo.framework.mybatis.mapper.entity.Example;
import com.icinfo.framework.tools.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 描述: 案源登记流程记录表 case_source_flow_record 对应的Service接口实现类.<br>
 *
 * @author framework generator
 * @date 2022年08月12日
 */
@Service
public class CaseSourceFlowRecordServiceImpl extends MyBatisServiceSupport implements ICaseSourceFlowRecordService {
    /**
     * 日志记录器
     */
    private static final Logger logger = LoggerFactory.getLogger(CaseSourceFlowRecordServiceImpl.class);

    @Autowired
    private CaseSourceFlowRecordMapper caseSourceFlowRecordMapper;
    /**
     * 查询流转日志
     * @author liyafeng
     * @date 2022/8/15
      * @param flowRecordDto
     * @return {@link List< FlowRecordVo>}
     */
    @Override
    public List<FlowRecordVo> getFlowRecordList(FlowRecordDto flowRecordDto) {
        List<FlowRecordVo> flowRecordVos=caseSourceFlowRecordMapper.selectFlowRecordList(flowRecordDto);
        return flowRecordVos;
    }
    /**
     *通用日志保存
     * @author liyafeng
     * @date 2022/8/15
      * @param flowCommonDto
     * @return {@link int}
     */
    @Override
    public int saveCommonFlowRecord(FlowCommonDto flowCommonDto) {
        CaseSourceFlowRecord  caseSourceFlowRecord=new CaseSourceFlowRecord();
        caseSourceFlowRecord.setCaseSourceId(flowCommonDto.getCaseSourceId());
        caseSourceFlowRecord.setId(StringUtils.uuid());
        caseSourceFlowRecord.setFlowEndTime(flowCommonDto.getFlowEndTime());
        caseSourceFlowRecord.setFlowStartTime(flowCommonDto.getFlowStartTime());
        caseSourceFlowRecord.setFlowStageType(flowCommonDto.getFlowStageType());
        caseSourceFlowRecord.setFlowStageName(FlowStageTypeEnum.getValue(flowCommonDto.getFlowStageType()));
        StringBuffer flowStageContent = new StringBuffer("");
        //案源登记、立案审批、不予立案审批、立案审批通过、不予立案审批通过 通用拼接 flow_stage_content
        List<String> stageTypeList = new ArrayList<String>(Arrays.asList(Constant.CASE_SOURCE_STAGE_AYDJ, Constant.CASE_SOURCE_STAGE_LASP,Constant.CASE_SOURCE_STAGE_BYLASP, Constant.CASE_SOURCE_STAGE_LASPTG,Constant.CASE_SOURCE_STAGE_BYLASPTG));
        if(stageTypeList.contains(flowCommonDto.getFlowStageType())){
            if(Constant.CASE_SOURCE_STAGE_LASP.equals(flowCommonDto.getFlowStageType())||flowCommonDto.getFlowStageType().equals(Constant.CASE_SOURCE_STAGE_BYLASP)){
                flowStageContent.append("提请人:");
            }
            if(Constant.CASE_SOURCE_STAGE_LASPTG.equals(flowCommonDto.getFlowStageType())||flowCommonDto.getFlowStageType().equals(Constant.CASE_SOURCE_STAGE_BYLASPTG)){
                flowStageContent.append("审批人:");
            }
            flowStageContent.append(flowCommonDto.getStartPersonName()+"("+flowCommonDto.getStartPersonOrgName()+"/"+flowCommonDto.getStartPersonDeptName()+")");
        }else if(Constant.CASE_SOURCE_STAGE_AYFP.equals(flowCommonDto.getFlowStageType())){
            flowStageContent.append("分派人:"+flowCommonDto.getStartPersonName()+"("+flowCommonDto.getStartPersonOrgName()+"/"+flowCommonDto.getStartPersonDeptName()+");");
            flowStageContent.append("被分派对象："+flowCommonDto.getEndOrgName()+";");
            if(StringUtils.isNotBlank(flowCommonDto.getCommonExplain())){
                flowStageContent.append("分派说明:"+flowCommonDto.getCommonExplain());
            }
        }else if(Constant.CASE_SOURCE_STAGE_AYZP.equals(flowCommonDto.getFlowStageType())){
            flowStageContent.append("指派人:"+flowCommonDto.getStartPersonName()+"("+flowCommonDto.getStartPersonOrgName()+"/"+flowCommonDto.getStartPersonDeptName()+");");
            flowStageContent.append("被指派对象:");
            for (AppointedPersonInfo appointedMainPersonInfo:flowCommonDto.getAppointedPersonInfoList()) {
                flowStageContent.append(appointedMainPersonInfo.getPersonName()+"("+appointedMainPersonInfo.getPersonOrgName()+"/"+appointedMainPersonInfo.getPersonDeptName()+");");
            }
            if(StringUtils.isNotBlank(flowCommonDto.getCommonExplain())){
                flowStageContent.append("指派说明:"+flowCommonDto.getCommonExplain());
            }
        }else if(Constant.CASE_SOURCE_STAGE_AYYS.equals(flowCommonDto.getFlowStageType())){
            flowStageContent.append("移送人:"+flowCommonDto.getStartPersonName()+"("+flowCommonDto.getStartPersonOrgName()+"/"+flowCommonDto.getStartPersonDeptName()+");");
            flowStageContent.append("移送单位："+flowCommonDto.getEndOrgName()+";");
            if(StringUtils.isNotBlank(flowCommonDto.getCommonExplain())){
                flowStageContent.append("移送说明:"+flowCommonDto.getCommonExplain());
            }
        }
        caseSourceFlowRecord.setFlowStageContent(flowStageContent.toString());
        int num=caseSourceFlowRecordMapper.insert(caseSourceFlowRecord);
        return num;
    }
    /**
     *查询最新的流转状态
     * @author liyafeng
     * @date 2022/8/15
      * @param
     * @return {@link CaseSourceFlowRecord}
     */
    @Override
    public CaseSourceFlowRecord getLastCaseSourceFlowRecord(String caseSourceId) {
        Example example =new Example(CaseSourceFlowRecord.class);
        example.createCriteria().andEqualTo("caseSourceId",caseSourceId);
        example.setOrderByClause("flow_end_time desc");
        List<CaseSourceFlowRecord> caseSourceFlowRecords=caseSourceFlowRecordMapper.selectByExample(example);
        if(null!=caseSourceFlowRecords&&caseSourceFlowRecords.size()>0){
            CaseSourceFlowRecord caseSourceFlowRecord=caseSourceFlowRecords.get(0);
            return caseSourceFlowRecord;
        }
        return null;
    }
}